<template>
	<!-- 怡家版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('public.materialCode')" />
		<!-- 搜索无记录 -->
		<zy-empty v-if="this.infoList.length==0"></zy-empty>
		<!-- 查询表单 -->
		<view class="container" v-for="(item,index) in infoList" :key="index" v-else>
			<zy-group :title="$t('public.batchNo')+$t('public.colon') + item.batchNo" :btnList="btnList"
				@btnHandle="delHandle(item, index)" model="insideCard">
				<view class="text">
					<text class="sub">{{$t('homepage.WarehouseManage.materialNum')}}</text>
					<view class="subContent">{{item.num}}{{$t('public.piece')}}</view>
				</view>
				<view class="text">
					<text class="sub">{{$t('public.materialCode')}}</text>
					<view class="subContent">{{item.materialCode}}</view>
				</view>
				<view class="text">
					<text class="sub">{{$t('public.materialName')}}</text>
					<view class="subContent">{{item.materialName}}</view>
				</view>
				<view class="text">
					<text class="sub">{{$t('homepage.WarehouseManage.materialAttribute')}}</text>
					<view class="subContent">{{item.attribute}}</view>
				</view>
			</zy-group>
		</view>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('homepage.WarehouseManage.select')"
			@rightBtn="selectHandler" @leftBtn="resetBtn" :leftDisabled="infoList.length>0?false:true"
			:rightDisabled="infoList.length>0?false:true">
		</bottomBtn>
		<!-- 筛选弹窗 -->
		<zy-popup ref="zyPopup" type='dialog' :title="$t('homepage.WarehouseManage.SPselect')" @cancel="cancel"
			@confirm="submit" closeable :closeOnClickOverlay="false">
			<zy-forms :model="model" labelWidth="80">
				<zy-form-item :label="$t('public.storage')">
					<zy-input v-model="model.storage" :placeholder="$t('public.pleaseInput')+$t('public.storage')"
						@confirm="storageSearch" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanStorage"></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('public.position')">
					<zy-input v-model="model.position" @confirm="positionSearch"
						:placeholder="$t('public.pleaseInput')+$t('public.position')" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanPosition"></i>
					</zy-input>
				</zy-form-item>
				<!-- <zy-form-item :label="$t('homepage.WarehouseManage.type')">
					<picker mode="selector" @change="typeChange" :value="typeIndex" :range="typeColumn"
						:range-key="'label'">
						<view class="formItem" @click="typeBtn">
							<input class="formInput" v-model="model.type" disabled
								:placeholder="$t('public.pleaseSelect')+$t('homepage.WarehouseManage.type')" />
							<i class="iconfont icon-xiangxia1"></i>
						</view>
					</picker>
				</zy-form-item> -->
			</zy-forms>
		</zy-popup>
		<!-- 入库提交二次确认 -->
		<zy-popup ref="popup" :title="$t('public.instorageSure')" type="dialog" @cancel="cancelBtn"
			@confirm="submitBtn">
			{{$t('public.isSure')}}{{model.type}}{{$t('public.question')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		getStorehouseDetailByCode,
		positionGetEntityByCode,
		storageGetEntityByCode,
		queryDictByType,
		savePosition,
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				// 列表删除按钮数据
				btnList: [{
					text: this.$t('public.del'),
					type: 'danger'
				}],
				iconfont: 'iconfont icon-saoyisao',
				model: {
					position: '',
					storage: '',
					// type: this.$t('public.goodsWarehousing'),
				},
				typeColumn: [], //类型下拉框数据
				typeIndex: 0,
				codeId: '',
				infoList: [],
				positionId: '',
				storageId: '',
				typeValue: ''
			}
		},
		onLoad() {
			this.getscancodedate()
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
			uni.$off('scancodepopdate')
		},
		methods: {
			//主页获取箱号扫描事件
			getscancodedate() {
				// 移除监听“添加扫描”弹窗事件
				uni.$off('scancodepopdate')
				//PDA扫描主页指示单号
				var _this = this
				uni.$on('scancodedate', function(data) {
					_this.searchList(data.code)
				})
			},
			//“弹窗储位”获取扫描事件
			getscancodepopdate() {
				// 移除监听“指示单号”事件
				uni.$off('scancodedate')
				//PDA扫描“添加扫描”弹窗事件
				var _this = this
				uni.$on('scancodepopdate', function(data) {
					_this.model.storage = data.code
					_this.storageSearch()
				})
			},
			//查询物料编码事件接口
			searchList(e) {
				getStorehouseDetailByCode({
					code: e,
				}).then((res) => {
					if (res.code == 0) {
						if (res.data.status == 0) {
							if (this.infoList.findIndex(item => item.barcode === e) !== -1) {
								uni.showToast({
									title: this.$t('public.searchRepeat'),
									icon: "none",
									duration: 3000
								})
							} else {
								this.infoList.push(res.data)
							}
						} else {
							let message = ""
							if (res.data.status == 1) {
								message = this.$t('public.inStoraged')
							} else if (res.data.status == 2) {
								message = this.$t('public.outStoraged')
							} else if (res.data.status == 3) {
								message = this.$t('homepage.WarehouseManage.canceled')
							}
							uni.showToast({
								title: this.$t('homepage.WarehouseManage.thiscode') + message,
								icon: "none",
								duration: 3000
							})
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 列表删除事件
			delHandle(item, index) {
				this.infoList.splice(index, 1);
			},
			// 底部仓/储按钮事件
			selectHandler() {
				this.$refs.zyPopup.open()
				this.getscancodepopdate()
			},
			//仓位扫描事件
			scanPosition() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.position = ret.resp_result;
							this.positionSearch()
						}
					})
			},
			//储位扫描事件
			scanStorage() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.storage = ret.resp_result;
							this.storageSearch()
						}
					})
			},
			//仓位接口
			positionSearch() {
				positionGetEntityByCode({
					code: this.model.position
				}).then((res) => {
					if (res.code == 0) {
						this.positionId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//储位接口
			storageSearch() {
				storageGetEntityByCode({
					code: this.model.storage
				}).then((res) => {
					if (res.code == 0) {
						this.model.position = res.data.warehousePosition.code
						this.positionSearch()
						this.storageId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// //类型按钮
			// typeBtn() {
			// 	this.typeColumn = []
			// 	queryDictByType({
			// 		type: "storehouse_in_type"
			// 	}).then((res) => {
			// 		for (var i = 0; i < res.data.length; i++) {
			// 			let obj = {}
			// 			obj.value = res.data[i].value
			// 			obj.label = res.data[i].label
			// 			this.typeColumn.push(obj)
			// 		}
			// 	}).catch((res) => {
			// 		uni.showToast({
			// 			title: res.message,
			// 			icon: "none",
			// 			duration: 3000
			// 		})
			// 	});
			// },
			// //类型选择器完成事件
			// typeChange(e) {
			// 	this.typeColumn.forEach((item, index) => {
			// 		if (e.detail.value == index) {
			// 			this.typeValue = item.value
			// 			this.model.type = item.label
			// 		}
			// 	})
			// },
			// 页面重置
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/StorageManagement/Instorage/index"
				})
			},
			// 仓/储选择提交
			submit() {
				this.$refs.popup.open()
			},
			// 仓/储选择取消
			cancel() {
				this.model.position = ''
				this.model.storage = ''
				this.positionId = ''
				this.storageId = ''
			},
			//入库提交二次确认
			submitBtn() {
				for (var i = 0; i < this.infoList.length; i++) {
					this.codeId += this.infoList[i].id + ','
				}
				savePosition({
					warehouseInId: this.codeId,
					warehousePositionId: this.positionId,
					warehouseStorageId: this.storageId,
					billType: this.typeValue
				}).then((res) => {
					if (res.code == 0) {
						uni.redirectTo({
							url: "/pages/HomePage/WarehouseManage/StorageManagement/Instorage/index"
						})
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//入库提交二次确认取消
			cancelBtn() {
				this.$refs.zyPopup.open()
				this.$refs.popup.close()
			},
		}
	}
</script>
<style lang="scss" scoped>
	.text {
		padding: 10rpx 0;
		width: 100%;
		display: flex;
		text-align: justify;
	}

	.sub {
		text-align: left;
		width: 160rpx;
		padding: 0 20rpx;
	}

	.subContent {
		display: flex;
		padding-right: 20rpx;
		width: calc(100% - 220rpx);
		color: #999999;
	}

	.formItem {
		width: calc(100% - 40rpx);
		padding: 20rpx;
		display: flex;
		border: 1px solid #e9e9e9;
		border-radius: 10rpx;
		background-color: #f5f5f5;
	}

	.formInput {
		margin: auto 0;
		width: 100%;
		border: none;
		outline: none;
		font-size: 32rpx;
		z-index: 999999;
	}

	.iconfont {
		margin: auto 0 auto 10rpx;
		font-size: 28rpx;
		color: #999999;
	}
</style>